const express = require('express');
const app = express();
const path = require('path');
const fs = require('fs');
const mysql = require('mysql');
app.listen(3000, () => { console.log('服务器已开启') });
app.get('/', (req, res) => { res.sendFile(path.join(__dirname, '3.百度.html')) });

//引入封装mySquery模块
const myQuery = require('./1.db');

app.get('/register', async (req, res) => {
    //接参
    console.log(req.query);
    let { username } = req.query;
    //sql数据库查询用户账号是否存在
    let sql = `SELECT * FROM aemo1 WHERE username = '${username}';`;

    let [err, result] = await myQuery(sql);
    if (result.length > 0) {
        res.send({ code: 400, msg: '用户已存在' });
        return;
    }

    //不存在就添加
    // let sql1 = `insert into from aemo1 (username) values ('${username}')`;
    // let [err1,result1] =  await myQuery(sql1);
    // if(err1){
    //     res.send({code:400,msg:'请重试'});
    //     return;
    // }

    // if(result1.insertId > 0){
    //     req.send({code:200,msg:'注册成功'})
    // }else{
    //     req.send({code:404,msg:'注册失败'})
    // }

})